<template>
  <div class="app-container">
    <el-card class="box-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <span class="card-title">用户意见列表</span>
        </div>
      </template>
      
      <!-- 搜索区域 -->
      <div class="search-container">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true">
          <el-form-item label="用户名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入用户名称"
              clearable
              @keyup.enter="handleQuery"
              @clear="handleQuery"
              class="search-input"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        border
        stripe
        highlight-current-row
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
      >
        <el-table-column
          prop="id"
          label="ID"
          
        />
        <el-table-column
          prop="userId"
          label="用户ID"
          width="200"
        />
        <el-table-column
          prop="content"
          label="意见内容"
          min-width="300"
        />
    
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="180"
        />
        <el-table-column
          prop="updateTime"
          label="更新时间"
          width="180"
        />
     
      </el-table>
      
      <el-pagination
        class="pagination"
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 30, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <!-- 查看/处理对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      width="500px"
      append-to-body
      destroy-on-close
      :close-on-click-modal="false"
    >
      <el-form
        ref="formRef"
        :model="form"
        label-width="80px"
        class="advice-form"
      >
        <el-form-item label="ID">
          <span class="form-text">{{ form.id }}</span>
        </el-form-item>
        <el-form-item label="用户ID">
          <span class="form-text">{{ form.userId }}</span>
        </el-form-item>
        <el-form-item label="意见内容">
          <div class="content-box">{{ form.content }}</div>
        </el-form-item>
        <el-form-item label="创建时间">
          <span class="form-text">{{ form.createTime }}</span>
        </el-form-item>
        <el-form-item label="更新时间">
          <span class="form-text">{{ form.updateTime }}</span>
        </el-form-item>
        <el-form-item label="处理备注" v-if="form.status === 0">
          <el-input 
            v-model="form.remark" 
            type="textarea" 
            :rows="4"
            placeholder="请输入处理备注" 
            maxlength="200"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="处理备注" v-else>
          <div class="content-box">{{ form.remark || '暂无备注' }}</div>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm" v-if="form.status === 0">确 定</el-button>
          <el-button @click="cancel">{{ form.status === 0 ? '取 消' : '关 闭' }}</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { listUserAdvice, getUserAdvice, processUserAdvice } from '@/api/advice/user'

// 遮罩层
const loading = ref(false)
// 总条数
const total = ref(0)
// 弹出层标题
const title = ref('')
// 是否显示弹出层
const open = ref(false)

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  name: ''
})

// 表单参数
const form = reactive({
  id: undefined,
  userId: '',
  content: '',
  status: 0,
  createTime: '',
  updateTime: '',
  remark: ''
})

// 表格数据
const tableData = ref([])

// 表单ref
const formRef = ref()
// 查询表单ref
const queryFormRef = ref()

/** 查询列表 */
function getList() {
  loading.value = true
  listUserAdvice(queryParams).then(response => {
    tableData.value = response.data.list
    total.value = response.data.total
    loading.value = false
  }).catch(() => {
    loading.value = false
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 表单重置 */
function reset() {
  form.id = undefined
  form.userId = ''
  form.content = ''
  form.status = 0
  form.createTime = ''
  form.updateTime = ''
  form.remark = ''
}

/** 查看按钮操作 */
function handleView(row) {
  reset()
  const id = row.id
  getUserAdvice(id).then(response => {
    Object.assign(form, response.data)
    open.value = true
    title.value = '查看意见详情'
  })
}

/** 处理按钮操作 */
function handleProcess(row) {
  reset()
  const id = row.id
  getUserAdvice(id).then(response => {
    Object.assign(form, response.data)
    open.value = true
    title.value = '处理用户意见'
  })
}

/** 提交按钮 */
function submitForm() {
  if (!form.remark) {
    ElMessage.warning('请输入处理备注')
    return
  }
  processUserAdvice(form).then(() => {
    open.value = false
    getList()
    ElMessage.success('处理成功')
  })
}

/** 分页大小改变 */
function handleSizeChange(val) {
  queryParams.pageSize = val
  getList()
}

/** 分页页码改变 */
function handleCurrentChange(val) {
  queryParams.pageNum = val
  getList()
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  queryFormRef.value?.resetFields()
  queryParams.pageNum = 1
  getList()
}

// 初始化数据
getList()
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.card-title {
  font-size: 18px;
  font-weight: bold;
  color: #303133;
  position: relative;
  padding-left: 12px;
}
.card-title::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 16px;
  background-color: #409EFF;
  border-radius: 2px;
}
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
.advice-form {
  padding: 10px;
}
.form-text {
  color: #606266;
}
.content-box {
  padding: 10px;
  background-color: #f5f7fa;
  border-radius: 4px;
  min-height: 40px;
  color: #606266;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-all;
}
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.search-container {
  background-color: #f8f9fb;
  border-radius: 4px;
  padding: 18px;
  margin-bottom: 20px;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.03);
}

.search-input {
  width: 240px;
}

.search-container :deep(.el-form-item) {
  margin-bottom: 0;
  margin-right: 20px;
}

.search-container :deep(.el-button) {
  transition: all 0.3s;
}

.search-container :deep(.el-button:hover) {
  transform: translateY(-2px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
</style>